package 
{
	import flash.display.Loader;
	import flash.display.Sprite;
	import flash.display.StageAlign;
	import flash.display.StageScaleMode;
	import flash.events.Event;
	import flash.events.KeyboardEvent;
	import flash.events.MouseEvent;
	import flash.geom.Point;
	import flash.net.URLLoader;
	import flash.net.URLRequest;
	import flash.utils.setTimeout;
	import ginger.geom.RectangleShape;
	import ginger.ui.simple.SimpleLayout;
	import caurina.transitions.Tweener;
	import caurina.transitions.properties.ColorShortcuts;
	import ginger.utils.NetUtil;
	import hwsoft.webservice_2_0.utils.ParseUtil;

	/**
	 * ...
	 * @author Ning
	 */
	public class Wall extends Sprite 
	{
		private var actionObjects:Array;
		private var _radius:Number =500;

		private var _objctNum:uint = 20;
		private var _actionObject:ActionObject;
		private var _holder:Sprite;
		private var _hoaderMask:RectangleShape;
		private var _hoaderBox:Sprite;
		private var _ruler:Ruler;
		
		private var _mouseHolder:Boolean;
		
		//摩擦力
		private var _friction:Number = 0.4; 
		//缓动系数
		private var _easing:Number = 0.1;
		//目标角度
		private var _targetRotation:Number = 0;
		private function set targetRotation(value:Number):void
		{
			//if (value > 30)
			    //_targetRotation = 30;
			//else if (value < -45)
			    //_targetRotation = -45;
			//else 
			    _targetRotation = value;
		}
		/**
		 * 设置目标角度百分比
		 */
		public function set targetRotationPer(value:Number):void
		{
			this.targetRotation = -75 * value+30;
		}
		//速度
		private var _v:Number = 2;
		
		

		public function Wall():void 
		{
			if (stage) onAddedToStage();
			else addEventListener(Event.ADDED_TO_STAGE, onAddedToStage);
		}

		private function onAddedToStage(e:Event = null):void 
		{
			removeEventListener(Event.ADDED_TO_STAGE, onAddedToStage);
			// entry point
			setTimeout(init, 200);
		}
		private function init():void
		{
			stage.align = StageAlign.TOP_LEFT;
			stage.scaleMode = StageScaleMode.NO_SCALE;
			
			Global.STAGE = this.stage;
			
			var loader:URLLoader = new URLLoader();
			loader.addEventListener(Event.COMPLETE, onComplete);
			
			var obj:Object = NetUtil.getFlashVarsParam(this.root, 'config');
			var url:String = obj == null?'asset/data.xml':String(obj);
			
			loader.load(new URLRequest(url));
		}
		private function onComplete(evt:Event):void
		{
			var data:Array = ParseUtil.parse(XML(evt.target.data), PhotoVO);
			
			_hoaderBox = new Sprite();
			
			
			_holder = new Sprite();
			_hoaderMask = new RectangleShape(1400, 500);
			
			_hoaderBox.mask = _hoaderMask
			addChild(_hoaderMask);
			
			
			_hoaderBox.addChild(_holder);
			addChild(_hoaderBox);
			
			_ruler = new Ruler(this);
			addChild(_ruler);
			
			
			actionObjects = [];
		
			for (var i:int = 0; i < _objctNum; i++ )
			{
				var angle:Number = Math.PI*2 / _objctNum * i;
				var data1:PhotoVO = data[i];
				var actionObject:ActionObject = new ActionObject(data1);
				actionObject.addEventListener(ActionObject.OVER, onOver);
				actionObject.addEventListener(ActionObject.OUT, onOut);
				actionObject.x = Math.cos(angle) * _radius;
				actionObject.z = Math.sin(angle) * _radius;
				actionObject.rotationY = -360 / _objctNum * i+90;
				_holder.addChild(actionObject);
				actionObjects.push(actionObject);
			}
			
			Tweener.addTween(this, { time:1.5, alpha:1 } );
			
			
			addEventListener(Event.ENTER_FRAME, onEnterFrame);
			_holder.z = 500;
			stage.addEventListener(Event.RESIZE, onResize);
			onResize();
			this.alpha = 0;
			_holder.addEventListener(MouseEvent.ROLL_OVER, onHolerOver);
			_holder.addEventListener(MouseEvent.ROLL_OUT, onHolerOut);
			stage.addEventListener(KeyboardEvent.KEY_DOWN, onKeyDown);
			stage.addEventListener(MouseEvent.MOUSE_WHEEL, onWheel);
		}
		private function onOver(evt:Event):void
		{
		//	Tweener.addTween(_holder, { time:1.5, z: -300 } );
			var target:ActionObject = evt.target as ActionObject;
			for (var i:int = 0; i < actionObjects.length; i++ )
			{
				var ao:ActionObject = actionObjects[i] as ActionObject;
				if (target != ao) {
					ColorShortcuts.init();
				    Tweener.addTween(ao, { time:1.5,_saturation:0} );
				}
			}
			
			target.parent.swapChildrenAt(target.parent.getChildIndex(target), target.parent.numChildren - 1);
		}
		private function onOut(evt:Event):void
		{
		//	Tweener.addTween(_holder, { time:1.5, z: -400 } );
			var target:ActionObject = evt.target as ActionObject;
			for (var i:int = 0; i < _objctNum; i++ )
			{
				var ao:ActionObject = actionObjects[i] as ActionObject;
				if (target != ao) {
					ColorShortcuts.init();
					Tweener.addTween(ao, { time:1.5,_saturation:1} );
				}
			}
		}
		private function onEnterFrame(evt:Event):void
		{
			if (_mouseHolder)
			{
				if (mouseX < stage.stageWidth / 3)
				    _ruler.moveSlider(-7);
				else if (mouseX > stage.stageWidth * 2 / 3)
				    _ruler.moveSlider(7);
			}
			
			if (!isNaN(_targetRotation)) {
				var dy:Number = _targetRotation - _holder.rotationY;
				//距离和速度成反比
				_v = dy * _easing;
			}
			else {
				//使用磨擦力(速度*磨擦力)
			    _v *= _friction;
			}
			_holder.rotationY += _v;
		}
		private function onKeyDown(evt:KeyboardEvent):void
		{
			if (evt.keyCode == 37)
			    _ruler.moveSlider(-20);
			else if (evt.keyCode == 39)
			    _ruler.moveSlider(20);
		}
		private function onWheel(evt:MouseEvent):void
		{
			if(evt.delta>0)
			    _ruler.moveSlider(10);
			else 
			    _ruler.moveSlider(-10);
		}
		private function onResize(evt:Event=null):void
		{
			_hoaderBox.x = stage.stageWidth / 2;
			_hoaderBox.y = stage.stageHeight / 2;

			SimpleLayout.layout(_hoaderMask, stage);
			SimpleLayout.layout(_ruler, stage);
			_ruler.y += 175;
		}
		private function onHolerOver(evt:MouseEvent):void
		{
			_mouseHolder = true;
		}
		private function onHolerOut(evt:MouseEvent):void
		{
			_mouseHolder = false;
		}
	}
}